package system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.controller.base.BaseController;
import common.model.po.SysRole;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import system.model.request.SaveRoleRequest;
import system.model.request.SaveUserRoleRequest;
import system.model.request.SearchRoleRequest;
import system.service.SysRoleService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author 米泽鹏
 * @since 2022-03-11
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/system/sys-role")
public class SysRoleController extends BaseController {

	@Resource
	private SysRoleService roleService;

	@ApiOperation(value = "角色分页查询", notes = "角色分页查询")
	@PostMapping("/readOnly/pageRole")
	public Result<Page<SysRole>> pageRole(@ApiParam(value = "分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchRoleRequest, SysRole> paginationRequest) {
		return successWithData(roleService.pageRole(paginationRequest));
	}

	@ApiOperation(value = "角色新增或更新", notes = "角色新增或更新")
	@PostMapping("/saveRole")
	public Result<?> saveRole(@ApiParam(value = "角色新增或更新请求", required = true) @RequestBody @Validated SaveRoleRequest request) {
		return roleService.saveRole(request);
	}

	@ApiOperation(value = "角色删除", notes = "角色删除")
	@DeleteMapping("/deleteRole/{id}")
	public Result<?> deleteRole(@ApiParam(value = "角色id", required = true) @PathVariable Long id) {
		return roleService.deleteRole(id);
	}

	@ApiOperation(value = "给角色分配用户", notes = "给角色分配用户", response = Result.class)
	@PostMapping("/saveUserRole")
	public Result<?> saveUserRole(@ApiParam(value = "给角色分配用户请求", required = true) @RequestBody @Validated SaveUserRoleRequest request) {
		return roleService.saveUserRole(request);
	}

	@ApiOperation(value = "获取用户角色信息", notes = "获取用户角色信息", response = Result.class)
	@GetMapping("/readOnly/getUserRoleInfo/{roleId}")
	public Result<Map<String, List<?>>> getUserRoleInfo(@ApiParam(value = "角色id", required = true) @PathVariable @Validated Long roleId) {
		return roleService.getUserRoleInfo(roleId);
	}

}
